//
//  UIWindow+Category.m
//  分享demo
//
//  Created by pc-chtypd056 on 2018/8/14.
//  Copyright © 2018年 CTL. All rights reserved.
//

#import "UIWindow+Category.h"
#import <objc/runtime.h>
#import <Foundation/Foundation.h>

@interface UIWindow()

@property (nonatomic, strong) UIView *backgroundView;

/** 最上层弹框 */
@property (nonatomic, strong) UIView *topPopView;

@end

@implementation UIWindow (Category)

- (UIView *)backgroundView{
    UIView *backgroundView = objc_getAssociatedObject(self, _cmd);
    if (!backgroundView)
    {
        backgroundView = [[UIView alloc] initWithFrame:self.bounds];
        self.backgroundView = backgroundView;
    }
    return backgroundView;
}

- (void)setBackgroundView:(UIView *)backgroundView{
    objc_setAssociatedObject(self, @selector(backgroundView), backgroundView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIView *)topPopView{
    return objc_getAssociatedObject(self, _cmd);
}

- (void)setTopPopView:(UIView *)topPopView{
    objc_setAssociatedObject(self, @selector(topPopView), topPopView, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (void)checkRemoveBackgroundView{
    if (self.backgroundView.subviews.count == 0)
    {
        [self.backgroundView removeFromSuperview];
    }
}

- (void)checkAddBackgroundView{
    if (![self.subviews containsObject:self.backgroundView])
    {
        [self addSubview:self.backgroundView];
    }
}

- (void)detectionScreenshots:(CompleteStr)actionComplete{
    
    [self checkAddBackgroundView];
    
    self.backgroundView.frame = CGRectMake(ScreenWIDTH-110, ScreenHEIGH/3, 100, 180);
    UIView *bgView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 100, 110)];
    bgView.backgroundColor = [UIColor clearColor];
    [self.backgroundView addSubview:bgView];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(5, 5, 100-10, 100)];
    imageView.image = [Tools imageWithScreenshot];
    bgView.backgroundColor = [UIColor blackColor];
    imageView.contentMode = UIViewContentModeScaleToFill;
    [bgView addSubview:imageView];
    
    __weak typeof(self) ws = self;
    void (^ AlertRemoveBlock)(void) = ^(){
        [ws removeAllPopBox];
    };
    
    NSArray *buttonArray = @[@"反馈问题",@"分享"];
    CGFloat btnH = 35;
    for (int i = 0; i < buttonArray.count; ++i) {
        UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, (i*40)+imageView.frame.size.height, self.backgroundView.frame.size.width, btnH);
        [button setTitle:buttonArray[i] forState:UIControlStateNormal];
        [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        button.backgroundColor = [UIColor blackColor];
        [self.backgroundView addSubview:button];
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(__kindof UIButton * _Nullable x) {
            AlertRemoveBlock();
            !actionComplete?:actionComplete(x.titleLabel.text);
        }];
    }
}

/**
 移除所有的弹框
 */
- (void)removeAllPopBox{
    
    if ([self.subviews containsObject:self.backgroundView])
    {
        [self.backgroundView.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
        [self.backgroundView removeFromSuperview];
        objc_removeAssociatedObjects(self);
    }
}

@end
